package com.ddogring.web.controller.ws;

import com.ddogring.framework.ws.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @BelongsProject: ddogring3.83
 * @BelongsPackage: com.ddogring.web.controller.ws
 * @Author: DdogRing
 * @CreateTime: 2022-09-13  16:05
 * @Description: TODO
 * @Version: 1.0
 */
@Controller
@RequestMapping("/wms/websocket")
public class WebSocketController {

    private String prefix = "wms/websocket";

    @GetMapping()
    public String socket() {
        return prefix + "/websocket"; // 页面的访问路径
    }

    //推送数据接口
    @ResponseBody
    @RequestMapping("/push/{cid}")
    public Map<String, Object> pushToWeb(@PathVariable String cid, String message) {
        if (message == null) {
            message = "陈佳欣推送成功" + new Random().nextInt(100);
        }
        Map<String, Object> result = new HashMap<>();
        try {
            WebSocketServer.sendInfo(message, cid);
            result.put("code", 200);
            result.put("msg", "success");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
